package com.example.saifly;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.common.Activity;
import com.example.common.Retrofit.RetrofitHelper;
import com.example.saifly.login.LoginActivity;
import com.example.saifly.mainPage.MainPageActivity;
import com.example.saifly.step.StepService;

import java.io.IOException;
import java.util.logging.LogRecord;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

public class MainActivity extends Activity {

//    private Messenger mGetReplyMessenger = new Messenger(new Handler(this));

//    ServiceConnection conn = new ServiceConnection() {
//        @Override
//        public void onServiceConnected(ComponentName name, IBinder service) {
//            try {
//                messenger = new Messenger(service);
//                Message msg = Message.obtain(null, Constant.MSG_FROM_CLIENT);
//                msg.replyTo = mGetReplyMessenger;
//                messenger.send(msg);
//            } catch (RemoteException e) {
//                e.printStackTrace();
//            }
//        }
//
//        @Override
//        public void onServiceDisconnected(ComponentName name) {
//
//        }
//    };


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void init() {


        SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
        Log.d("uidddd", "init: "+preferences.getString("uid", "1"));
        if (preferences.getString("uid", "1").equals("1")) {
            startActivity(new Intent(this, LoginActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    verifyCookie();
                }
            }).start();
        }
    }

    private interface ApiVerifyCookie{
        @Headers({"Content-Type:application/json"})
        @POST("/login/check")
        Call<ResponseBody> registerVerifyVerify(@Body JSONObject jsonObject);
    }

    private void verifyCookie(){
        RetrofitHelper retrofitHelper=new RetrofitHelper();
        Retrofit retrofit=retrofitHelper.getRetrofit();

        ApiVerifyCookie apiVerifyCookie=retrofit.create(ApiVerifyCookie.class);

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",1);

        Call<ResponseBody> call=apiVerifyCookie.registerVerifyVerify(jsonObject);
        final Response<ResponseBody> response;
        Log.d("registerhaha", "sendVerifyRequest: enteringhaha");
        try {
            response=call.execute();
            if(response.body()!=null){
                JSONObject result= JSON.parseObject(response.body().string());
                if(result.getInteger("code")==4002){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this,"登陆信息已过期，请重新登录",Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(MainActivity.this,LoginActivity.class)
                                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
                        }
                    });
                }else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            startActivity(new Intent(MainActivity.this, MainPageActivity.class)
                                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
                        }
                    });
                }
            }else{
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,"无网络连接",Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(MainActivity.this,LoginActivity.class)
                                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
                    }
                });
            }
        } catch (IOException e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this,"无网络连接",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(MainActivity.this,LoginActivity.class)
                            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
                }
            });
            e.printStackTrace();
        }
    }
}
